package com.fieldchain.ex.fa100.utils.net.modle;

import android.util.Log;

import com.fieldchain.ex.fa100.bean.event.BaseEvent;
import com.fieldchain.ex.fa100.utils.net.inter.BaseRequest;

import org.greenrobot.eventbus.EventBus;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * Created by biyunlong on 2016/10/14.
 */

public abstract class StringRequest extends BaseRequest {
    private int code;
    private String msg;
    @Override
    public void baseOnSucess(String jsonstr,String tags) {
        Log.e("StringRequest",jsonstr);
        if(jsonstr.startsWith("{")){
            JSONObject jsonRoot = null;
            try {
                jsonRoot = new JSONObject(jsonstr);
                code=jsonRoot.getInt("code");
                msg=jsonRoot.getString("msg");
                EventBus.getDefault().post(new BaseEvent(code,tags));
            } catch (JSONException e) {
                e.printStackTrace();
            }

        }
        if(msg!=null){
            onSucess(jsonstr,code,msg);
        }else{
            onSucess(jsonstr,0,"操作失败");
        }

    }

    @Override
    public void baseOnFaild(Exception e,String tags) {
        onFaild(e);

    }

    public abstract void onSucess(String str, int code, String msg);

    public abstract void onFaild(Exception e);
}
